package top.luoqiz.jenkins.config.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import top.luoqiz.jenkins.config.sms.SmsCodeAuthenticationSecurityConfig;

/**
 * @author luoqiz
 */
@Configuration
public class JenkinsWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

    @Autowired
    private AuthenticationSuccessHandler authenticationSuccessHandler;

    @Autowired
    private AuthenticationFailureHandler authenticationFailureHandler;

    @Autowired
    private LogoutSuccessHandler logoutSuccessHandler;

    @Autowired
    private LogoutHandler logoutHandler;

    @Autowired
    private SmsCodeAuthenticationSecurityConfig smsCodeAuthenticationSecurityConfig;

    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        http.authorizeRequests((requests) ->
//                requests.antMatchers(
//                        "/",
//                        "/login",
//                        "/jenkins/code",
//                        "/oauth/**",
//                        "/v2/api-docs",
//                        "/configuration/**",
//                        "/swagger*/**",
//                        "/webjars/**", "/**/*.js", "/**/*.css", "/**/*.png"
//                ).permitAll());
        // Since we want the protected resources to be accessible in the UI as well we need
        // session creation to be allowed (it's disabled by default in 2.0.6)
        http.formLogin(Customizer.withDefaults())
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED);

        //关闭默认的csrf认证
        http.csrf().disable();
//        http.formLogin();//.successHandler(authenticationSuccessHandler).failureHandler(authenticationFailureHandler);
//        http.logout(logout -> logout.logoutSuccessHandler(logoutSuccessHandler).addLogoutHandler(logoutHandler)
//                .invalidateHttpSession(true).deleteCookies());
//        http.apply(smsCodeAuthenticationSecurityConfig);
//        http.oauth2Login().successHandler(authenticationSuccessHandler).failureHandler(authenticationFailureHandler)
//                .userInfoEndpoint();
//        http.oauth2Client(oauth2 -> oauth2
//                .clientRegistrationRepository(this.clientRegistrationRepository())
//                .authorizedClientRepository(this.authorizedClientRepository())
//                .authorizedClientService(this.authorizedClientService())
//                .authorizationCodeGrant(codeGrant -> codeGrant
//                        .authorizationRequestRepository(this.authorizationRequestRepository())
//                        .authorizationRequestResolver(this.authorizationRequestResolver())
//                        .accessTokenResponseClient(this.accessTokenResponseClient())
//                )
//        );
    }

}